package com.seari.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.seari.bean.PersonInfoBean;
import com.seari.bean.ReqPlanShiftBean;
import com.seari.scheduling.plan.PlanFourClass;
import com.seari.scheduling.plan.PlanMonMidClass;
import com.seari.scheduling.plan.PlanMonNigClass;

@Service
public class PlanSchedulingServiceImpl implements PlanSchedulingService
{

	@Autowired
	PlanFourClass fourClassInstance;

	@Autowired
	PlanMonMidClass monMidClassinstance;

	@Autowired
	PlanMonNigClass monNigClassinstance;

	@Autowired
	PerSonInfoService perSonInfoService;

	@Override
	public List<Object> fetchPlanSchedulingViewData(ReqPlanShiftBean bean)
	{
		List<PersonInfoBean> perSonInfoList = perSonInfoService.fetchPerSonInfoList();

		Map<String, List<PersonInfoBean>> classify = new HashMap<>();
		
		//按照岗位对人员分类
		for (PersonInfoBean personInfoBean : perSonInfoList)
		{
			if (classify.containsKey(personInfoBean.getPostInfo()))
			{
				classify.get(personInfoBean.getPostInfo()).add(personInfoBean);
			} else
			{
				List<PersonInfoBean> list = new ArrayList<>();
				list.add(personInfoBean);
				classify.put(personInfoBean.getPostInfo(), list);
			}
		}

		List<Object> res = new ArrayList<>();
		
		Iterator<Entry<String, List<PersonInfoBean>>> iterator = classify.entrySet().iterator();
		while (iterator.hasNext())
		{
			Map<String,Object> item = new HashMap<>();
			Entry<String, List<PersonInfoBean>> next = iterator.next();
			List<PersonInfoBean> value = next.getValue();
			String type = value.get(0).getShift();
			switch (type)
			{
			case "fourClass":
				List<PersonInfoBean> fourClassData = fourClassInstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", fourClassData);
				res.add(item);
				break;

			case "monMid":
				List<PersonInfoBean> monMidData = monMidClassinstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", monMidData);
				res.add(item);
				break;

			case "monNig":
				List<PersonInfoBean> monNigData = monNigClassinstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", monNigData);
				res.add(item);
				break;
			default:
				break;
			}
		}
		
		String jsonString = JSON.toJSONString(res);
		System.out.println(jsonString);
		
		return res;
	}
}
